package com.lu.manage.modular.system.controller;

import com.lu.manage.core.base.controller.BaseController;
import com.lu.manage.core.common.constant.SystemConstant;
import com.lu.manage.core.common.global.SystemCache;
import com.lu.manage.core.common.node.ZTreeNode;
import com.lu.manage.core.shiro.ShiroKit;
import com.lu.manage.core.tips.SuccessTip;
import com.lu.manage.core.utils.ToolUtil;
import com.lu.manage.modular.system.model.User;
import com.lu.manage.modular.system.service.DeptService;
import com.lu.manage.modular.system.service.RoleService;
import com.lu.manage.modular.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program LuBoot
 * @description: 用户控制器
 * @author: zhanglu
 * @create: 2019-11-05 20:13:00
 */
@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

    private static String PREFIX = "/modular/system/user";

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;


/*******************************跳转页面相关*******************************/

    /**
     * 首页
     * @return
     */
    @GetMapping("")
    public Object index(){
        return PREFIX + "/user.html";
    }

    /**
     * 新增
     * @return
     */
    @GetMapping("/toAdd")
    public Object toAdd(){
        return PREFIX + "/user_add.html";
    }

    /**
     * 修改
     * @return
     */
    @GetMapping("/toUpdate")
    public Object toUpdate(String id, Model model){
        User user = userService.getById(id);
        user.setDeptName(SystemCache.deptMap.get(user.getDeptId()).getSimpleName());
        model.addAttribute("obj", user);
        return PREFIX + "/user_update.html";
    }

    /**
     * 修改密码
     * @return
     */
    @GetMapping("/toUpdatePwd")
    public Object toUpdatePwd(){
        return "/modular/frame/password.html";
    }


/*******************************操作按钮相关*******************************/

    /**
     * 新增用户
     * @param user
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @ResponseBody
    @RequiresPermissions(value = "/system/user/add")
    public Object add(User user) throws Exception{
        this.userService.add(user);
        return new SuccessTip();
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    @RequiresPermissions(value = "/system/user/update")
    public Object update(User user) throws Exception {

        //如果修改了编号，则该用户的子用户也要修改对应的父级(用户层级修改情况)
        this.userService.update(user);

        return new SuccessTip();
    }

    /**
     * 修改当前用户的密码
     *
     * @author fengshuonan
     * @Date 2018/12/24 22:43
     */
    @PostMapping("/changePwd")
    @ResponseBody
    @RequiresPermissions(value = "/system/user/changePwd")
    public Object changePwd(String oldPassword, String newPassword) throws Exception {
        this.userService.changePwd(oldPassword, newPassword);
        return new SuccessTip();
    }


    /**
     * 删除用户
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = "/system/user/delete")
    public Object delete(String id) throws Exception{
        this.userService.delete(id);
        return new SuccessTip();
    }


    /**
     * 分配角色
     * @param id
     * @param roleIds
     * @return
     * @throws Exception
     */
    @PostMapping("/setRole")
    @ResponseBody
    @RequiresPermissions(value = "/system/user/setRole")
    public Object setRole(String id, String roleIds) throws Exception {
        //不能修改超级管理员
        if (id.equals(SystemConstant.systemSupperAdminId)) {
            throw new Exception("不能修改超级管理员角色");
        }
        this.userService.setRoles(id, roleIds);
        return new SuccessTip();
    }

    /**
     * 重置密码
     * @param id
     * @return
     */
    @PostMapping("/reset")
    @ResponseBody
    @RequiresPermissions(value = "/system/user/reset")
    public Object reset(String id) {
        User user = this.userService.getById(id);
        user.setSalt(ShiroKit.getRandomSalt(5));
        user.setPassword(ShiroKit.md5(SystemConstant.DEFAULT_PWD, user.getSalt()));
        this.userService.updateById(user);
        return new SuccessTip();
    }



/*******************************查询数据相关*******************************/

    /**
     * 用户列表分页
     * @param map 查询参数
     * @return
     */
    @GetMapping("/listPage")
    @ResponseBody
    public Object listPage(@RequestParam Map<String, Object> map){
        return userService.listPage(map);
    }

    /**
     * 部门树
     * 超级管理员可以看全部
     * 其他用户只能看到自己
     * @return
     */
    @GetMapping("/deptLayuiTreeByUser")
    @ResponseBody
    public Object deptLayuiTreeByUser(){
        return deptService.deptLayuiTree();
//        if(ShiroKit.isAdmin()){
//            return deptService.deptLayuiTree();
//        }else {
//            return deptService.deptLayuiTreeByPid(ShiroKit.getUser().getDeptId());
//        }
    }

    /**
     * 获取角色列表，通过用户id
     * @param id
     * @return
     */
    @GetMapping("/roleTreeListByUserId/{id}")
    @ResponseBody
    public List<ZTreeNode> roleTreeListByUserId(@PathVariable String id) {
        User theUser = this.userService.getById(id);
        String roleId = theUser.getRoleId();
        if (ToolUtil.isEmpty(roleId)) {
            return (List<ZTreeNode>) this.roleService.roleTreeList();
        } else {

            String[] strArray = roleId.split(",");
            List<ZTreeNode> zTreeNodeList = this.roleService.roleTreeListByRoleId(strArray);
            return zTreeNodeList;
        }
    }

}
